home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Mac-Source 1994 July
/
Mac-Source_July_1994.iso
/
C and C++
/
Text⁄Files
/
File List 14
/
Window.c
< prev
next >
Wrap
Text File
|
1990-09-14
|
7KB
|
328 lines
/*
FileList 1.4
"Window.c"
*/
#include "Main.h"
#include "Mouse.h"
#include "Stack.h"
#include "Search.h"
#include "Window.h"
#define BAR 15 /* Scroll bar width - 1 */
#define LEFT 3 /* Left margin of text in window */
/* ----- Invert rectangle using current hilite color ------------------- */
pascal void myInvertRect (Rect *) = {
0x08B8, 0x0007, 0x0938, /* BCLR #hiliteBit,HiliteMode */
0xA8A4 }; /* _InvertRect */
/* ----- Return content rectangle -------------------------------------- */
void ContentRect (
register WindowPtr w,
register Rect *r)
{
*r = w->portRect;
r->top += BAR;
r->bottom -= BAR;
r->right -= BAR;
}
/* ----- Return content + header rectangle ----------------------------- */
void ContentHeaderRect (
register WindowPtr w,
register Rect *r)
{
*r = w->portRect;
r->bottom -= BAR;
r->right -= BAR;
}
/* ----- Return vertical scroll bar rectangle -------------------------- */
void VScrollRect (
register WindowPtr w,
register Rect *r)
{
*r = w->portRect;
r->top -= 1;
r->bottom -= BAR - 1;
r->right +=1;
r->left = r->right - BAR - 1;
}
/* ----- Return horizontal scroll bar rectangle ------------------------ */
void HScrollRect (
register WindowPtr w,
register Rect *r)
{
*r = w->portRect;
r->bottom += 1;
r->top = r->bottom - BAR - 1;
r->left -= 1;
r->right -= BAR - 1;
}
/* ----- Return header line rectangle ---------------------------------- */
void HeaderRect (
register WindowPtr w,
register Rect *r)
{
*r = w->portRect;
r->bottom = r->top + BAR;
r->right -= BAR;
}
/* ----- Return a correct window port rectangle ------------------------ */
void WPortRect (
register short v,
register short h,
register Rect *r)
{
r->bottom = r->top + v*((r->bottom - r->top - 2*BAR)/v) + 2*BAR;
r->right = r->left + h*((r->right - r->left - BAR)/h) + BAR + LEFT;
}
/* ----- Set window size ----------------------------------------------- */
void SetWindowSize (
register WindowDataPtr w,
register short width,
register short heigth)
{
Rect r;
SetPort((WindowPtr)w);
EraseRect(&((WindowPtr)w)->portRect);
r.top = r.left = 0;
r.bottom = heigth;
r.right = width;
WPortRect(w->height, w->width, &r);
SizeWindow(w, r.right - r.left, r.bottom - r.top, TRUE);
InvalRect(&((WindowPtr)w)->portRect);
FixScrollBar(w); /* Resize window's scroll bar */
AdjustScrollBar(w); /* Adjust scroll bars to size of document */
}
/* ----- Redraw one line only (used by vertical scroll) ---------------- */
void RedrawV1 (
register WindowDataPtr p,
short dir)
{
register short y, n;
register long v;
register unsigned char string[256];
Rect r;
ContentRect((WindowPtr)p, &r);
if ((n = (r.bottom - r.top)/p->height - 1) < 0) /* Lines - 1 */
return;
v = GetCtlValue(p->vs); /* First line */
y = r.top + p->height - p->descent; /* Position of first line */
if (dir < 0) {
v += n; /* Last Line */
y += n * p->height; /* Position of last line */
}
if (v >= p->count)
return;
SetPort(p);
MoveTo(LEFT - GetCtlValue(p->hs) * p->width, y);
(*(p->redraw))(v, string);
DrawString(string);
if (Selected(p, v)) {
r.bottom = y + p->descent;
r.top = r.bottom - p->height;
myInvertRect(&r);
}
}
/* ----- Reformat and redraw all window data --------------------------- */
void RedrawV (
register WindowDataPtr p,
Boolean full)
{
register short x, y, n;
register long v;
register unsigned char string[256];
register StringHandle title;
Rect r;
RgnHandle clip;
SetPort(p);
x = LEFT - GetCtlValue(p->hs) * p->width;
HeaderRect((WindowPtr)p, &r);
MoveTo(0, r.bottom - 1);
LineTo(r.right, r.bottom - 1); /* Draw separation line */
MoveTo(x, r.bottom - p->descent - 2);
if (title = GetString(p->header)) {
HLock(title);
DrawString(*title);
HUnlock(title);
}
ContentRect((WindowPtr)p, &r);
if ((n = (r.bottom - r.top)/p->height) <= 0) /* Lines */
return;
if (full) {
clip = ((WindowPtr)p)->clipRgn;
((WindowPtr)p)->clipRgn = NewRgn();
RectRgn(((WindowPtr)p)->clipRgn, &r);
}
v = GetCtlValue(p->vs); /* First line */
y = r.top + p->height - p->descent; /* Position of first line */
while (n--) {
if (v >= p->count)
break;
MoveTo(x, y);
(*(p->redraw))(v, string);
DrawString(string);
if (Selected(p, v) && p == (WindowDataPtr)FrontWindow()) {
r.bottom = y + p->descent;
r.top = r.bottom - p->height;
myInvertRect(&r);
}
y += p->height;
v++;
}
if (full) {
DisposeRgn(((WindowPtr)p)->clipRgn);
((WindowPtr)p)->clipRgn = clip;
}
}
/* ----- Draw selection ------------------------------------------------ */
void DrawSelection (register WindowDataPtr p)
{
register long i;
register long n1, n2;
Rect r;
if ((i = p->select) == -1L)
return;
ContentRect((WindowPtr)p, &r);
n2 = (n1 = GetCtlValue(p->vs)) + (r.bottom - r.top)/p->height;
if (n2 >= p->count)
n2 = p->count;
if (n1 <= i && i < n2) { /* If it is visible */
r.top += (i - n1) * p->height;
r.bottom = r.top + p->height;
InvalRect(&r);
}
}
/* ----- Toggle selection ---------------------------------------------- */
void ToggleIt (
register WindowDataPtr p,
register long i)
{
register long n1, n2;
Rect r;
ContentRect((WindowPtr)p, &r);
n2 = (n1 = GetCtlValue(p->vs)) + (r.bottom - r.top)/p->height;
if (n2 >= p->count)
n2 = p->count;
if (i < p->count)
Toggle(p, i);
if (n1 <= i && i < n2) { /* If it is visible */
r.top += (i - n1) * p->height;
r.bottom = r.top + p->height;
myInvertRect(&r);
}
}
/* ----- Find selection ------------------------------------------------ */
static long FindIt (
register WindowDataPtr p,
Point position)
{
register long v;
Rect r;
ContentRect((WindowPtr)p, &r);
v = GetCtlValue(p->vs) + (position.v - r.top)/p->height;
if (v >= p->count)
return -1L;
return v;
}
/* ----- Row selection ------------------------------------------------- */
void FindRow (
register WindowDataPtr p,
Point position) /* Local coordinates */
{
register long v;
RgnHandle clip;
Rect r;
SetPort(p);
ContentRect((WindowPtr)p, &r);
clip = ((WindowPtr)p)->clipRgn;
((WindowPtr)p)->clipRgn = NewRgn();
RectRgn(((WindowPtr)p)->clipRgn, &r);
if ((v = FindIt(p, position)) != -1L) {
if (p->select != v && p->select != -1L)
ToggleIt(p, p->select);
ToggleIt(p, v);
}
DisposeRgn(((WindowPtr)p)->clipRgn);
((WindowPtr)p)->clipRgn = clip;
}
/* ----- Restore original window position and size --------------------- */
void RestoreWindow (register WindowDataPtr p)
{
SetPort(p);
HideWindow(p);
MoveWindow(p, p->rectangle.left, p->rectangle.top, TRUE);
SetWindowSize(p,
p->rectangle.right - p->rectangle.left,
p->rectangle.bottom - p->rectangle.top);
ShowWindow(p);
}
/* ----- Move to selection --------------------------------------------- */
void MoveToLine (register WindowDataPtr w, unsigned long v)
{
register short y1, y2;
Rect r;
y1 = GetCtlValue(w->vs);
ContentRect((WindowPtr)w, &r);
y2 = y1 + (r.bottom - r.top)/w->height;
if (v < y1 || v >= y2) {
SetCtlValue(w->vs, v - (y2 - y1)/2);
SetPort(w);
InvalRect(&r);
}
}
/* ----- Move to selection --------------------------------------------- */
void Selection (void)
{
register unsigned long v;
register WindowDataPtr w = (WindowDataPtr)FrontWindow();
if ((v = w->select) == -1)
return;
MoveToLine(w, v);
}